package com.example.demo53.ui.Setting.ip;


import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.demo53.MainActivity;
import com.example.demo53.R;
import com.example.demo53.methods.net.NetWorkUtil;
import com.example.demo53.methods.net.boardcast_ip;
import com.example.demo53.methods.net.receive_message;
import com.example.demo53.ui.Group.GroupFragment;
import com.example.demo53.util.Adapter.GridViewAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import static android.view.View.inflate;

import androidx.annotation.RequiresApi;


/**
 * 负责监听setting界面的点击事件
 */
public class NetPairing extends MainActivity {

    private EditText tv_editcode;
    private EditText tv_editport;
    private EditText tv_editssid;
    private TextView ip_getRoute;
    private TextView ip_sendroute;
    private int PORT =8080;
    private String route_ip;
    private String code;
    private String ssid;
    private GridView mGridView;
    private GridViewAdapter adapter;



    private List<Integer> mDatas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //View view = View.inflate(this,R.layout.fragment_group,null);
        setContentView(R.layout.config_net);
        ip_getRoute =findViewById(R.id.ip_getroute);
        ip_sendroute = findViewById(R.id.ip_sendroute);
        tv_editport = findViewById(R.id.tv_editport);
        tv_editcode = findViewById(R.id.tv_editcode);
        tv_editssid = findViewById(R.id.tv_editssid);
       // mGridView = findViewById(R.id.gv1);

        setListen();
        //initDatas();

    }

    private void setListen() {

        ip_getRoute.setOnClickListener(new View.OnClickListener() {
            @Override
            //------------------获取路由ip
            public void onClick(View v) {
////--------------------以下两种方法都能判断传入的int值是否为空
//                try {
//                    PORT= Integer.parseInt(tv_editport.getText().toString());
//                      if (PORT==0){
//                          PORT=8080;
//                                  }
//                } catch (NumberFormatException e) {
//                    //错误则默认8080
//                    PORT=8080;                }
//
//                code= tv_editcode.getText().toString().trim();//trim是去重文本的空格
//
////---------------------第二种方法
//                //String port1 = ""+PORT;
////                if (tv_editport.getText().toString()!=null&&tv_editport.getText().toString().length()!=0){
////                    PORT=Integer.parseInt(tv_editport.getText().toString());
////                }
//                if(code == null || code.length() == 0){
//                    code="00000000";
//                }
//                ssid = tv_editssid.getText().toString().trim();
//                if(ssid == null || ssid.length() == 0){
//                    ssid="";
//                }
                route_ip= NetWorkUtil.getWifiRouteIPAddress(NetPairing.this);
//                //============获取ssid的方法====================
//               //已经抛弃
//                boardcast_ip a = new boardcast_ip();
//                a.initdata_ip(route_ip,PORT,code,ssid);
//                a.start();

                final Toast toast = Toast.makeText(NetPairing.this,"ip："+route_ip, Toast.LENGTH_LONG);
                showTimeToast(toast,3000);
//----------------跳转页面（已注释
//                mip = tv_editip.getText().toString();
//                Intent i = new Intent(confignet.this, SettingFragment.class);
//                startActivity(i);

            }
        });




//---------------------发送获得的ip
        ip_sendroute.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//--------------------以下两种方法都能判断传入的int值是否为空
                try {
                    PORT= Integer.parseInt(tv_editport.getText().toString());
                    if (PORT==0){
                        PORT=8080;
                    }
                } catch (NumberFormatException e) {
                    //错误则默认8080
                    PORT=8080;                }

                code= tv_editcode.getText().toString().trim();//trim是去重文本的空格

//---------------------第二种方法
                //String port1 = ""+PORT;
//                if (tv_editport.getText().toString()!=null&&tv_editport.getText().toString().length()!=0){
//                    PORT=Integer.parseInt(tv_editport.getText().toString());
//                }
                if(code == null || code.length() == 0){
                    code="00000000";
                }
                ssid = tv_editssid.getText().toString().trim();
                if(ssid == null || ssid.length() == 0){
                    ssid="";
                }
                route_ip= NetWorkUtil.getWifiRouteIPAddress(NetPairing.this);
                //============获取ssid的方法====================
                //已经抛弃
                boardcast_ip a = new boardcast_ip();
                a.initdata_ip(route_ip,PORT,code,ssid);
                a.start();

                final Toast toast = Toast.makeText(NetPairing.this,"已传送配置信息....", Toast.LENGTH_LONG);
                showTimeToast(toast,3000);
            }
        });

    }


    public static void showTimeToast(final Toast toast, final int time) {
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                toast.show();
            }
        }, 0, 1000);//delay：持续时间 效果不明显
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                toast.cancel();
                timer.cancel();
            }
        }, time);//time:延迟显现toast时间
    }
/**
 * 以下为获取ssid的方法，
 * 因为权限不足而作废，用手动输入方式获取
 */
//    private String getSSID(){
//        WifiManager  mWifiManager = (WifiManager) this.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
//        ConnectivityManager  mConnectivityManager = (ConnectivityManager) this.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
//        NetworkInfo wifiInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
//        String wifiName = wifiInfo.getExtraInfo();
////        if (wifiName.startsWith("\"")) {
////            wifiName = wifiName.substring(1, wifiName.length());
////        }
////        if (wifiName.endsWith("\"")) {
////            wifiName = wifiName.substring(0, wifiName.length() - 1);
////        }
//        return wifiName;
//    }
//
//
//    public static String getWIFISSID(Activity activity) {
//        String ssid = "unknown id";
//
//        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.O || Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
//
//            WifiManager mWifiManager = (WifiManager) activity.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
//
//            assert mWifiManager != null;
//            WifiInfo info = mWifiManager.getConnectionInfo();
//            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
//                return info.getSSID();
//            } else {
//                return info.getSSID().replace("\"", "");
//            }
//        } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O_MR1) {
//
//            ConnectivityManager connManager = (ConnectivityManager) activity.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
//            assert connManager != null;
//            NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
//            if (networkInfo.isConnected()) {
//                if (networkInfo.getExtraInfo() != null) {
//                    return networkInfo.getExtraInfo().replace("\"", "");
//                }
//            }
//        }
//        return ssid;
//    }
////
////    private String getSSIDByNetworkId(Context context) {
////        String ssid;
////        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
////        if (wifiMgr != null) {
////            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
////            int networkId = wifiInfo.getNetworkId();
////            List<WifiConfiguration> configuredNetworks = wifiMgr.getConfiguredNetworks();
////            for (WifiConfiguration wifiConfiguration : configuredNetworks){
////                if (wifiConfiguration.networkId == networkId){
////                    ssid = wifiConfiguration.SSID;
////                    break;
////                }
////            }
////        }
////        return ssid;
//
//    public NetworkInfo getNetworkInfo(Context context){
//        try{
//            final ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
//            if (null != connectivityManager){
//                return connectivityManager.getActiveNetworkInfo();
//            }
//        }catch(Exception e){
//            e.printStackTrace();
//        }
//        return null;
//    }




    }

